package cs2340.droidceptiontodo.test;

import java.util.ArrayList;

import cs2340.droidceptiontodo.AddToDoItemActivity;
import cs2340.droidceptiontodo.Database;
import cs2340.droidceptiontodo.ToDoItem;
import cs2340.droidceptiontodo.UserAccount;
import android.test.ActivityInstrumentationTestCase2;
import android.test.AndroidTestCase;
import android.test.RenamingDelegatingContext;
import android.test.UiThreadTest;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddToDoItemActivityTest3 extends ActivityInstrumentationTestCase2<AddToDoItemActivity>{

	
	private static final String TEST_FILE_PREFIX = "test_";
	private AddToDoItemActivity activity;
	private TextView dateView, timeView;
	private Button chooseDateButton, clearDateButton, chooseTimeButton, clearTimeButton,
					addItemButton, cancelButton;
	private EditText nameField, descriptionField, categoryField, locationField;
	private UserAccount user;
	
	public AddToDoItemActivityTest3() {
		// Android uses this information to find the application and activity to test.
		super("cs2340.droidceptiontodo", AddToDoItemActivity.class);
	}
	
	protected void setUp() throws Exception {
		
		super.setUp();
		// Touch mode must be turned off in order to test
		// key events
		setActivityInitialTouchMode(false);
		// Gets reference to AddToDoItemActivity and starts activity
		activity = (AddToDoItemActivity) getActivity();
		
		
		// Get references to widgets under test
		dateView = (TextView) activity.findViewById(cs2340.droidceptiontodo.R.id.dateView);
		timeView = (TextView) activity.findViewById(cs2340.droidceptiontodo.R.id.timeView);
		chooseDateButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.chooseDateButton);
		clearDateButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.clearDateButton);
		chooseTimeButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.chooseTimeButton);
		clearTimeButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.clearTimeButton);		
		nameField = (EditText) activity.findViewById(cs2340.droidceptiontodo.R.id.name);
		descriptionField = (EditText)  activity.findViewById(cs2340.droidceptiontodo.R.id.description);
		categoryField = (EditText) activity.findViewById(cs2340.droidceptiontodo.R.id.category);
		locationField = (EditText) activity.findViewById(cs2340.droidceptiontodo.R.id.location);
		addItemButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.addItemButton);
		cancelButton = (Button) activity.findViewById(cs2340.droidceptiontodo.R.id.cancelButton);
		
		
	}
	
	@UiThreadTest
	public void testOnClick() {
		
		nameField.setText("New To-Do Item");
		descriptionField.setText("Item description goes here.");
		categoryField.setText("School");
		locationField.setText("Georgia Tech");
		
		// Get database from AddToDoItemActivity
		Database db = activity.getDatabase();
		db.open();
		// Delete item from previous test run
		db.deletebyItem("New To-Do Item");
		db.checkUser("roberta", "roberta");
		// click add button
		addItemButton.performClick();
		// addItem closes database, so it needs to be opened again
		db.open();
		// Get entire list of items from user
		ArrayList<ToDoItem> items = db.getItems("roberta");
		assertTrue("List of items is not empty: ", !items.isEmpty());
		// newItem has info entered above
		ToDoItem newItem = new ToDoItem("New To-Do Item", "roberta", "Item description goes here.", "School", 
				"","","Georgia Tech", false, null);
		// Find a to-do item with the same name to show it is in the database
		assertTrue("New To-Do Item is in list of items: ", items.get(items.size()-1).getName().equals("New To-Do Item"));
		
		// logout user and close database
		Database.currentUser = null;
		db.close();
		db = null;
		
	}
	
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	
}

